﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Expect.Core.Fixtures;
using EnvDTE;
using EnvDTE80;

namespace Expect.VisualStudio.Extensions.Commands.Handlers
{
    class CreateColumnFixtureClass : CreateFixtureClass
    {
        public CreateColumnFixtureClass(IExpectPackage pkg)
            : base(pkg, CommandIDs.cmdidCreateColumnFixtureClass)
        {
        }

        public override string DisplayText
        {
            get
            {
                return "Fixture class => create column fixture class";
            }
        }

        public override bool ShowCommand(SelectionResolution resolution)
        {
            if (
                base.ShowCommand(resolution) && 
                resolution.IsFixture && 
                resolution.Fixture.Rows.Count > 2 &&
                resolution.Fixture.RowsHaveSameNumberOfCells(1))
            {
                return true;
            }

            return false;
        }

        protected override Type GetClassType()
        {
            return typeof(ColumnFixture);
        }

        protected override void ProcessClass(SelectionResolution resolution, CodeClass generatedclass)
        {
            var rowDefinition = resolution.Fixture.Rows[1];

            foreach (var cell in rowDefinition.Cells)
            {
                var name = resolution.NameProvider.ElementName(cell.Text);
                if (resolution.NameProvider.IsAMethod(cell.Text))
                {
                    var function = (CodeFunction2)generatedclass.AddFunction(name, vsCMFunction.vsCMFunctionFunction, "string", -1, vsCMAccess.vsCMAccessPublic);
                }
            }

            var pt = generatedclass.GetStartPoint(vsCMPart.vsCMPartBody);
            var ptEdit = pt.CreateEditPoint();
            foreach (var cell in rowDefinition.Cells)
            {
                var name = resolution.NameProvider.ElementName(cell.Text);
                if (!resolution.NameProvider.IsAMethod(cell.Text))
                {                    
                    ptEdit.Insert("\t\tpublic string " + name + " { get; set;}\r\n\r\n");
                }
            }            
        }
    }
}
